package a10_动态规划;

import java.util.Arrays;

/**
 * <p>
 * a23_零钱兑换复习2
 * </p>
 *
 * @author flyduck
 * @since 2025/3/26
 */
public class a23_零钱兑换复习2 {
    public static void main(String[] args) {
        System.out.println(coinChange(new int[]{2}, 3));
    }

    //完全背包 顺时针
    //最小物品数量dp[j] = Math.min(dp[j],dp[j-coins[i]] + 1)

    //初始化：dp[0] = 0
    //其他下标都会进行运算：dp[j] = Math.min(dp[j],dp[j-coins[i]] + 1);
    //所以dp[j] = Integer.MAX_VALUE;
    public static int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount+1];
        for (int j = 0; j < dp.length; j++) {
            dp[j] = Integer.MAX_VALUE;
        }
        dp[0] = 0;

        for (int i = 0; i < coins.length; i++) {
            for (int j = coins[i]; j <= amount; j++) {
                if(dp[j - coins[i]] != Integer.MAX_VALUE){
                    dp[j] = Math.min(dp[j],dp[j-coins[i]] + 1);
                }
            }
        }
        return dp[amount] != Integer.MAX_VALUE ? dp[amount] : -1;
    }
}
